package com.atguigu.source;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.connector.file.src.FileSource;
import org.apache.flink.core.fs.Path;
import org.apache.flink.formats.csv.CsvReaderFormat;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvMapper;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.io.File;
import java.math.BigDecimal;
import java.util.function.Function;

import static org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.OnCheckpointRollingPolicy.build;

/**
 * @author yhm
 * @create 2024-04-01 15:29
 */
public class Test03_Csv {
    public static void main(String[] args) throws Exception {
        // 1. 创建环境
        Configuration conf = new Configuration();
        conf.setInteger("rest.port",8081);

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        // 2. 读取数据源
        CsvReaderFormat<CityPojo> csvFormat = CsvReaderFormat.forPojo(CityPojo.class);
        FileSource<CityPojo> source =
                FileSource.forRecordStreamFormat(csvFormat, Path.fromLocalFile(new File("input/csv.txt"))).build();

        DataStreamSource<CityPojo> streamSource = env.fromSource(source, WatermarkStrategy.noWatermarks(), "csv");

        // 3. 处理数据

        // 4. 输出
        streamSource.print();

        // 5. 执行环境
        env.execute();
    }

    @JsonPropertyOrder({"city","lat","lng","country","iso2",
            "adminName","capital","population"})
    public static class CityPojo {
        public String city;
        public BigDecimal lat;
        public BigDecimal lng;
        public String country;
        public String iso2;
        public String adminName;
        public String capital;
        public long population;

        @Override
        public String toString() {
            return "CityPojo{" +
                    "city='" + city + '\'' +
                    ", lat=" + lat +
                    ", lng=" + lng +
                    ", country='" + country + '\'' +
                    ", iso2='" + iso2 + '\'' +
                    ", adminName='" + adminName + '\'' +
                    ", capital='" + capital + '\'' +
                    ", population=" + population +
                    '}';
        }
    }
}
